package com.live.dengdengtv.fragment.hall;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.live.dengdengtv.R;
import com.live.dengdengtv.activity.SearchActivity;
import com.live.dengdengtv.activity.msg.MyMsgActivity;
import com.live.dengdengtv.fragment.BaseFragment;
import com.live.dengdengtv.view.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wyf on 2016/7/27.
 */
public class HallFragment extends BaseFragment implements View.OnClickListener{

    private static HallFragment hallFragent = new HallFragment();
    public static HallFragment getInstance(){
        return hallFragent;
    }

    private static class SamplePagerItem {
        private final CharSequence mTitle;
        private final int mIndicatorColor;
        private final int mDividerColor;

        SamplePagerItem(CharSequence title, int indicatorColor, int dividerColor) {
            mTitle = title;
            mIndicatorColor = indicatorColor;
            mDividerColor = dividerColor;
        }

        private Fragment createFragment() {

            if("最新".equals(mTitle)){
                return new RecentFragment();
            }else if("热门".equals(mTitle)){
                return new HotFragment();
            }else if("频道".equals(mTitle)){
                return new ChannelFragment();
            }
            return ContentFragment.newInstance(mTitle, mIndicatorColor, mDividerColor);
        }


        private CharSequence getTitle() {
            return mTitle;
        }


        private int getIndicatorColor() {
            return mIndicatorColor;
        }


        private int getDividerColor() {
            return mDividerColor;
        }
    }

    private static final String TAG = "HallFragment";


    private SlidingTabLayout mSlidingTabLayout;

    private ViewPager mViewPager;


    private List<SamplePagerItem> mTabs = new ArrayList<SamplePagerItem>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTabs.add(new SamplePagerItem(

                "最新", // Title
                0xFFED242C, // Indicator color
                0x00FFFFFF // Divider color
        ));

        mTabs.add(new SamplePagerItem(
                "热门", // Title
                0xFFED242C, // Indicator color
                0x00FFFFFF // Divider color
        ));

        mTabs.add(new SamplePagerItem(
                "频道", // Title
                0xFFED242C, // Indicator color
                0x00FFFFFF // Divider color
        ));
    }

    private View contentView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(contentView == null ) {
            contentView = inflater.inflate(R.layout.hall, container, false);
        }
        return contentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        ImageButton hal_msg_ib = (ImageButton) view.findViewById(R.id.hal_msg_ib);
        ImageButton hal_search_ib = (ImageButton) view.findViewById(R.id.hal_search_ib);
        hal_msg_ib.setOnClickListener(this);
        hal_search_ib.setOnClickListener(this);


        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mViewPager.setAdapter(new SampleFragmentPagerAdapter(getChildFragmentManager()));

        mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setCustomTabView(R.layout.hall_tabtv, R.id.tab_tv);
        final float density = getResources().getDisplayMetrics().density;
        mSlidingTabLayout.setNavigationThick((int) (density*3));
        mSlidingTabLayout.setDefaultTextColor(0xFFECE8E9);
        mSlidingTabLayout.setSelectedTextColor(0xFFED242C);


        mSlidingTabLayout.setViewPager(mViewPager);
        mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return mTabs.get(position).getIndicatorColor();
            }
            @Override
            public int getDividerColor(int position) {
                return mTabs.get(position).getDividerColor();
            }
        });

        mViewPager.setCurrentItem(1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.hal_search_ib :
                goSearch(); break;
            case R.id.hal_msg_ib :
                goMsgActivity(); break;
        }
    }

    private void goMsgActivity() {
        Intent intent = new Intent(getActivity(), MyMsgActivity.class);
        startActivity(intent);
    }

    private void goSearch() {
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        mTabs.clear();
        super.onDestroyView();
    }

    class SampleFragmentPagerAdapter extends FragmentPagerAdapter {

        SampleFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return mTabs.get(i).createFragment();
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTabs.get(position).getTitle();
        }
    }


}
